package com.example.androidaudiovideodemo

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout

/**
 *
 * author: renbing
 *
 * date: 2021/12/10
 *
 * des:
 */
class SimpleTitleView @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null,defStyle: Int = 0) : ConstraintLayout(context, attrs,defStyle){

    private var mLeftIconIv: ImageView? = null
    private var mCenterTextTv: TextView? = null
    private var mRightIconIv: ImageView? = null

    private var mLeftIconVisible: Boolean = true
    private var mRightIconVisible: Boolean = true
    private var mCenterTextVisible: Boolean = true

    private var mLeftIcon: Drawable? = null
    private var mCenterText: String? = null
    private var mRightIcon: Drawable? = null
    private var mLeftIconWidth: Int = 0
    private var mLeftIconHeight: Int = 0
    private var mRightIconWidth: Int = 0
    private var mRightIconHeight: Int = 0
    private var mCenterTextSize: Int = 0
    private var mCenterTextColor: Int = 0

    init {
        val ta = context.obtainStyledAttributes(attrs,R.styleable.SimpleTitleView)
        mLeftIconVisible = ta.getBoolean(R.styleable.SimpleTitleView_left_icon_visible,true)
        mRightIconVisible = ta.getBoolean(R.styleable.SimpleTitleView_right_icon_visible,true)
        mCenterTextVisible = ta.getBoolean(R.styleable.SimpleTitleView_center_text_visible,true)
        mLeftIcon = ta.getDrawable(R.styleable.SimpleTitleView_left_icon)
        mCenterText = ta.getString(R.styleable.SimpleTitleView_center_text)
        mRightIcon = ta.getDrawable(R.styleable.SimpleTitleView_right_icon)
        mLeftIconWidth = ta.getDimension(R.styleable.SimpleTitleView_left_icon_width,0f).toInt()
        mLeftIconHeight = ta.getDimension(R.styleable.SimpleTitleView_left_icon_height,0f).toInt()
        mRightIconWidth = ta.getDimension(R.styleable.SimpleTitleView_right_icon_width,0f).toInt()
        mRightIconHeight = ta.getDimension(R.styleable.SimpleTitleView_right_icon_height,0f).toInt()
        mCenterTextSize = ta.getDimensionPixelSize(R.styleable.SimpleTitleView_center_text_size,18)
        mCenterTextColor = ta.getColor(R.styleable.SimpleTitleView_center_text_color,Color.BLACK)
        ta.recycle()

        val titleView = LayoutInflater.from(context).inflate(R.layout.layout_simple_title_view,this)
        mLeftIconIv = titleView.findViewById(R.id.simple_title_view_left_iv)
        mCenterTextTv = titleView.findViewById(R.id.simple_title_view_center_tv)
        mRightIconIv = titleView.findViewById(R.id.simple_title_view_right_iv)

        if (mLeftIconWidth != 0){
            val layoutParams = mLeftIconIv?.layoutParams
            layoutParams?.width = mLeftIconWidth
            mLeftIconIv?.layoutParams = layoutParams
        }
        mLeftIconIv?.visibility = if (mLeftIconVisible) View.VISIBLE else View.GONE
        mRightIconIv?.visibility = if (mRightIconVisible) View.VISIBLE else View.GONE
        mCenterTextTv?.visibility = if (mCenterTextVisible) View.VISIBLE else View.GONE
        if (mLeftIcon != null){
            mLeftIconIv?.setImageDrawable(mLeftIcon)
        }
        if (mRightIcon != null){
            mRightIconIv?.setImageDrawable(mRightIcon)
        }
        if (mCenterText != null){
            mCenterTextTv?.text = mCenterText
            mCenterTextTv?.textSize = mCenterTextSize.toFloat()
            mCenterTextTv?.setTextColor(mCenterTextColor)
        }
    }

    fun setLeftIconColorFilter(color: Int){
        mLeftIconIv?.setColorFilter(color)
    }

    fun setOnLeftIconClickListener(listener:() ->Unit){
        mLeftIconIv?.setOnClickListener {
            listener.invoke()
        }
    }

    fun setOnRightIconClickListener(listener:() ->Unit){
        mRightIconIv?.setOnClickListener {
            listener.invoke()
        }
    }

    fun setCenterText(text: String){
        mCenterTextTv?.text = text
    }
}